var Toolbar = function() {
	var o = new Object();
	
	o.newDocumentUrl = "toolbar/document";
	o.newDirectoryUrl = "toolbar/directory";

	o.init = function(){
		o.bindSelector();
		o.bindEvents();
	}
	/**
	 * 返回上一级目录
	 * 
	 * @returns
	 */
	o.goBack = function() {
		if(currentDir.indexOf('/') != -1){
			currentDir = currentDir.substring(0,currentDir.lastIndexOf("/"));
		} else{
			currentDir= "";
		};
		enterDir(currentDir);
	}
	
	/**
	 * 返回跟目录
	 * 
	 * @returns
	 */
	o.goHome = function () {
		currentDir="";
		enterDir(currentDir);
	}
	/**
	 * 使用模态框上传文件
	 * 
	 * @returns
	 */
	o.goUpload = function () {
		o.uploadModal.on('hidden.bs.modal', function () {
			enterDir(currentDir);
		})
		o.uploadModal.modal()
	}
	
	o.newDocument = function(){
		log("新建文件",currentDir);
		var prefix = currentDir && currentDir +"/";
		o.newDocumentName.val(prefix);
		o.newDocumentModal.modal();
	}
	
	o.newDirectory = function(){
		log("新建目录")
		var prefix = currentDir && currentDir +"/";
		o.newDirectoryName.val(prefix);
		o.newDirectoryModal.modal();
	}
	
	var validateDoc = function(doc){
		var re= new RegExp('^([^\\\\\\?\\/\\*\\|<>:\"]+/)*([^\\\\\\?\\/\\*\\|<>:\"])*\\.\\w+$');
		return re.test(doc);
	}
	
	var validateDir = function(dir){
		var re= new RegExp('^(?:[^\\\\\\?\\/\\*\\|<>:\"]+/)*(?:[^\\\\\\?\\/\\*\\|<>:\"])+$');
		return re.test(dir);
	}
	o.newDocFormSubmit = function (){
		var txt = o.newDocumentName.val();
		log(txt)
		if(!validateDoc(txt)){	
			alert(txt +"不符合文件的命名规范");
			return false;
		}
		//名称校验
		//newDocumentName
		var ajaxOptions={
	         	 // 几个参数需要注意一下
	             type: "get",// 方法类型
	             dataType: "json",// 预期服务器返回的数据类型
	             url: o.newDocumentUrl ,// url
	             data: o.newDocumentForm.serialize(),
	             success: function (result) {
	                 log(result);// 打印服务端返回的数据(调试用)
	                 //关闭模态框
	                 o.newDocumentModal.modal('hide');
	                 if (result.code == 200) {
	                     enterDir(currentDir);
	                 }else{
	                	 log(result.message);
	                 }
	             },
	             error : function() {
	                 alert("异常！");
	             }
	         }
		 $.ajax(ajaxOptions);
	     
	}

	o.newDirFormSubmit = function (){
		
		var txt = o.newDirectoryName.val();
		log(txt)
		if(!validateDir(txt)){	
			alert(txt +"不符合文件的命名规范");
			return false;
		}
		var ajaxOptions = {
	        	 // 几个参数需要注意一下
	            type: "get",// 方法类型
	            dataType: "json",// 预期服务器返回的数据类型
	            url: o.newDirectoryUrl ,// url
	            data: o.newDirectoryForm.serialize(),
	            success: function (result) {
	                console.log(result);// 打印服务端返回的数据(调试用)
	                o.newDirectoryModal.modal('hide');
	                if (result.code == 200) {
	                    enterDir(currentDir);
	                }else{
	               	 	log(result.message);
	                }
	            },
	            error : function() {
	                alert("异常！");
	            }
	        };
		log(ajaxOptions);
		$.ajax(ajaxOptions);
	}

	o.bindSelector = function(){
		//input
		o.newDocumentName = $('#newDocumentName');
		o.newDocumentModal = $("#newDocumentModal");
		o.newDirectoryName = $('#newDirectoryName');
		o.newDirectoryModal = $('#newDirectoryModal');
		//form
		o.newDocumentForm = $('#newDocumentForm');
		o.newDocumentFormSubmit = $('#newDocumentFormSubmit');
		o.newDirectoryForm =  $('#newDirectoryForm');
		o.newDirectoryFormSubmit = $('#newDirectoryFormSubmit');
		
		//button
		o.goHomeBtn = $('#goHome');
		o.goBackBtn = $('#goBack');
		o.goUploadBtn = $('#goUpload');
		o.newDocumentBtn = $('#newDocument');
		o.newDirectoryBtn = $('#newDirectory');
		
		//upload
		o.uploadModal = $('#uploadModal');

	}
	o.bindEvents = function(){
		var self = o;
		o.goHomeBtn.click(function(){
			self.goHome();
		});
		
		o.goBackBtn.click(function(){
			self.goBack();
		});
		
		o.goUploadBtn.click(function(){
			self.goUpload();
		});
		o.newDocumentBtn.click(function(){
			self.newDocument();
		});
		o.newDirectoryBtn.click(function(){
			self.newDirectory();
		});
		o.newDocumentFormSubmit.click(function(){
			self.newDocFormSubmit();
		});
		log('newDirFormSubmit',o.newDirectoryFormSubmit)
		o.newDirectoryFormSubmit.click(function(){
			log('newDirFormSubmit')
			self.newDirFormSubmit();
		});
		
	}
	return o;
}